package com.msmall.community.admin.dao.hibernate;

import java.util.List;

import javax.persistence.Table;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import com.msmall.base.dao.hibernate.BaseDaoHibernate;
import com.msmall.community.admin.dao.AdminUserDao;
import com.msmall.community.admin.model.AdminUser;

public class AdminUserDaoHibernate extends BaseDaoHibernate<AdminUser, Long> implements AdminUserDao{

    public AdminUserDaoHibernate() {
        super(AdminUser.class);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<AdminUser> getUsers() {
        return getHibernateTemplate().find("from AdminUser u order by upper(u.username)");
    }

    /**
     * {@inheritDoc}
     */
    public AdminUser saveUser(AdminUser user) {
        log.debug("user's id: " + user.getUserId());
       	super.getHibernateTemplate().merge(user);
        return user;
    }

    @Override
    public AdminUser save(AdminUser user) {
        return this.saveUser(user);
    }

    /** 
     * {@inheritDoc}
    */
    public AdminUser loadUserByUsername(String username) throws Exception {
    	try{
	        List users = getHibernateTemplate().find("from AdminUser where username=?", username);
	        if (users == null || users.isEmpty()) {
	            return null;
	        } else {
	            return (AdminUser) users.get(0);
	        }
    	}catch(Exception e){
    		throw e;
    	}
    }
}
